package com.feng.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class ZuulPostFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "post";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

//    @Override
//    public boolean shouldFilter() {
//        return true;
//    }


    @Override
    public boolean shouldFilter() {
        final boolean b = RequestContext.getCurrentContext().sendZuulResponse();
        return b;
    }

    @Override
    public Object run() {
        log.info("ZuulPostFilter");

        int i = 1 / 0;

        try {
            RequestContext ctx = RequestContext.getCurrentContext();
//            ctx.getResponse().sendRedirect("http://baidu.com");
            // 获取响应body，转换成json格式
//            InputStream stream = ctx.getResponseDataStream();
//            String bodyStr = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
//            log.info("\n\norigin response body is: {}\n\n", bodyStr );
//            ctx.setResponseBody(bodyStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try {
//            // 获取当前请求基本信息
//            RequestContext ctx = RequestContext.getCurrentContext();
////            HttpServletRequest request = ctx.getRequest();
//            // 获取响应body，转换成json格式
//            InputStream stream = ctx.getResponseDataStream();
//            String bodyStr = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
//            log.info("\n\norigin response body is: {}\n\n", bodyStr );
//
//            // 获取userId
////            String userId = null;
////            Cookie[] cookies = request.getCookies();
////            if (cookies != null) {
////                for (Cookie cookie : cookies) {
////                    if ("userid".equals(cookie.getName())) {
////                        userId = cookie.getValue();
////                        break;
////                    }
////                }
////            }
//            // 设置响应的 cookie
////            HttpServletResponse response = ctx.getResponse();
////            List<Cookie> cookieLst = new ArrayList<>();
////            // 可以在这加一下你想加的东西，并加到响应的cookie中
////            if (cookieLst != null) {
////                for (Cookie cookie : cookieLst) {
////                    cookie.setPath("/");
////                    response.addCookie(cookie);
////                    log.info("post filter after {} : {}", cookie.getName(), cookie.getValue());
////                }
////            }
//        } catch (IOException ex) {
//            throw new RuntimeException(ex);
//        }

        return null;
    }
}